{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.rand(5,2),index=[\"a\",\"b\",\"c\",\"d\",\"e\"])\n",
    "reindex_df = df.reindex([\"a\",\"b\",\"c\",\"d\",\"e\",\"f\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1\n",
      "a  False  False\n",
      "b  False  False\n",
      "c  False  False\n",
      "d  False  False\n",
      "e  False  False\n",
      "f   True   True\n"
     ]
    }
   ],
   "source": [
    "# 查询缺失值\n",
    "print(reindex_df.isnull())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1\n",
      "a  0.779087  0.920907\n",
      "b  0.733549  0.642738\n",
      "c  0.970966  0.278022\n",
      "d  0.483610  0.339845\n",
      "e  0.792513  0.575965\n",
      "f  1.000000  1.000000\n"
     ]
    }
   ],
   "source": [
    "# 填充缺失值\n",
    "print(reindex_df.fillna(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1\n",
      "a  0.779087  0.920907\n",
      "b  0.733549  0.642738\n",
      "c  0.970966  0.278022\n",
      "d  0.483610  0.339845\n",
      "e  0.792513  0.575965\n",
      "f       NaN       NaN\n"
     ]
    }
   ],
   "source": [
    "# 替换缺失值\n",
    "print(reindex_df.replace({1.0:2.0}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1\n",
      "a  0.779087  0.920907\n",
      "b  0.733549  0.642738\n",
      "c  0.970966  0.278022\n",
      "d  0.483610  0.339845\n",
      "e  0.792513  0.575965\n"
     ]
    }
   ],
   "source": [
    "# 丢弃缺失值\n",
    "print(reindex_df.dropna())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
